package cn.edu.sgu.www.mhxysy.service.except.impl;

import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuJiadian;
import cn.edu.sgu.www.mhxysy.entity.except.JiadianSchema;
import cn.edu.sgu.www.mhxysy.entity.role.RoleAttribute;
import cn.edu.sgu.www.mhxysy.entity.role.RoleJiadian;
import cn.edu.sgu.www.mhxysy.enums.OpenStatus;
import cn.edu.sgu.www.mhxysy.enums.UnlockStatus;
import cn.edu.sgu.www.mhxysy.factory.impl.RoleAttributeFactory;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuJiadianMapper;
import cn.edu.sgu.www.mhxysy.mapper.except.JiadianSchemaMapper;
import cn.edu.sgu.www.mhxysy.mapper.role.RoleAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.role.RoleJiadianMapper;
import cn.edu.sgu.www.mhxysy.service.except.JiadianSchemaService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class JiadianSchemaServiceImpl implements JiadianSchemaService {

    private final RoleJiadianMapper roleJiadianMapper;
    private final RoleAttributeMapper roleAttributeMapper;
    private final JiadianSchemaMapper jiadianSchemaMapper;
    private final ChongwuJiadianMapper chongwuJiadianMapper;

    @Autowired
    public JiadianSchemaServiceImpl(
            RoleJiadianMapper roleJiadianMapper,
            RoleAttributeMapper roleAttributeMapper,
            JiadianSchemaMapper jiadianSchemaMapper,
            ChongwuJiadianMapper chongwuJiadianMapper) {
        this.roleJiadianMapper = roleJiadianMapper;
        this.roleAttributeMapper = roleAttributeMapper;
        this.jiadianSchemaMapper = jiadianSchemaMapper;
        this.chongwuJiadianMapper = chongwuJiadianMapper;
    }

    @Override
    public List<JiadianSchema> selectAll() {
        return jiadianSchemaMapper.selectList(null);
    }

    @Override
    public List<JiadianSchema> selectByRoleId(String roleId) {
        // 查询角色加点方案
        List<JiadianSchema> jiadianSchemas = jiadianSchemaMapper.selectByTypeName("角色加点");

        // 角色ID不为空
        if (StringUtils.isNotEmpty(roleId)) {
            // 查询角色加点信息
            Map<String, Object> columnMap = new HashMap<>();

            columnMap.put("role_id", roleId);

            List<RoleJiadian> list = roleJiadianMapper.selectByMap(columnMap);

            // 没有分配第四套加点，则添加第四套加点
            if (list.size() == 3) {
                RoleJiadian roleJiadian = list.get(0);
                String jiadianId = roleJiadian.getId();

                jiadianId = jiadianId.substring(0, jiadianId.length() - 1) + 4;

                /*
                 * 1、添加角色加点
                 */
                RoleJiadian jiadian = new RoleJiadian();

                // 得到之前3个角色加点的任意一个加点的ID，把最后一个数字改成4
                jiadian.setId(jiadianId);
                jiadian.setMoli(0);
                jiadian.setNaili(0);
                jiadian.setTizhi(0);
                jiadian.setMinjie(0);
                jiadian.setLiliang(0);
                jiadian.setRoleId(roleId);
                jiadian.setName("加点方案4");
                jiadian.setJiadianSchema(4);
                jiadian.setOpenStatus(OpenStatus.UN_OPEN.getValue());
                jiadian.setUnlockStatus(UnlockStatus.WJS.getValue());

                roleJiadianMapper.insert(jiadian);

                /*
                 * 2、添加角色属性
                 */
                // 通过角色ID和加点方案查询角色属性
                QueryWrapper<RoleAttribute> queryWrapper = new QueryWrapper<>();

                queryWrapper.eq("role_id", roleId);
                queryWrapper.eq("jiadian_id", jiadian.getId());

                RoleAttribute roleAttribute = roleAttributeMapper.selectOne(queryWrapper);

                // 得到之前3个角色加点的任意一个加点的属性ID，把最后一个数字改成4
                String attributeId = roleAttribute.getId();

                attributeId = attributeId.substring(0, attributeId.length() - 1) + 4;

                // 添加角色属性
                RoleAttributeFactory roleAttributeFactory = new RoleAttributeFactory();
                RoleAttribute produce = roleAttributeFactory.produce();

                produce.setId(attributeId);
                produce.setRoleId(roleId);
                produce.setJiadianId(jiadianId);

                roleAttributeMapper.insert(produce);

                // 重新查询最新的的角色加点
                list = roleJiadianMapper.selectByMap(columnMap);
            }

            /*
             * 遍历结果集，对加点方案名称进行对应修改
             */
            for (RoleJiadian jiadian : list) {
                for (JiadianSchema jiadianSchema : jiadianSchemas) {
                    if (jiadian.getJiadianSchema().equals(jiadianSchema.getId())) {
                        // 修改未解锁加点方案的加点名称
                        if (UnlockStatus.isUnlocked(jiadian.getUnlockStatus())) {
                            String unlockName = "开启新方案";

                            jiadianSchema.setName(unlockName);
                        } else { // 设置了加点名称的角色加点，使用设置的加点名称
                            String name = jiadian.getName();

                            if (StringUtils.isNotEmpty(name)) {
                                jiadianSchema.setName(name);
                            }
                        }
                    }
                }
            }
        }

        return jiadianSchemas;
    }

    @Override
    public List<JiadianSchema> selectByChongwuId(String chongwuId) {
        // 查询宠物加点方案
        List<JiadianSchema> list = jiadianSchemaMapper.selectByTypeName("宠物加点");

        // 宠物ID不为空
        if (StringUtils.isNotEmpty(chongwuId)) {
            // 查询角色加点信息
            QueryWrapper<ChongwuJiadian> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("chongwu_id", chongwuId);

            List<ChongwuJiadian> chongwuJiadians = chongwuJiadianMapper.selectList(queryWrapper);

            /*
             * 遍历结果集，对加点方案名称进行对应修改
             */
            for (ChongwuJiadian jiadian : chongwuJiadians) {
                for (JiadianSchema jiadianSchema : list) {
                    if (jiadian.getJiadianSchema().equals(jiadianSchema.getId())) {
                        // 修改未解锁加点方案的加点名称
                        if (UnlockStatus.isUnlocked(jiadian.getUnlockStatus())) {
                            String unlockName = "开启新方案";

                            jiadianSchema.setName(unlockName);
                        } else {
                            // 设置了加点名称的角色加点，使用设置的加点名称
                            String name = jiadian.getName();

                            if (StringUtils.isNotEmpty(name)) {
                                jiadianSchema.setName(name);
                            }
                        }
                    }
                }
            }
        }

        return list;
    }

}